﻿using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Membership;
using Ue.Qcdz.Domain.ResourceManager;

namespace Ue.Qcdz.UI.Controls
{
    public class UserRegister : SkinnedEntityEdit<User>
    {
        public static string userRegisterConfirmEmailTemplate;

        static UserRegister()
        {
            var templateUrl = SiteUrls.Instance.BuildUrl("userRegisterConfirmEmail");

            userRegisterConfirmEmailTemplate = File.ReadAllText(Ue.Framework.Web.UrlHelper.GetFilePath(templateUrl));
        }


        #region 成员字段和构造方法
        private TextBox tbxUserName;
        private TextBox tbxPassword;
        private TextBox tbxPasswordAg;
        private TextBox tbxCompany;
        private TextBox tbxCompanyAddress;
        private TextBox tbxTelCode;
        private TextBox tbxTelphone;
        private TextBox TbxMobile;
        private string strUid;
        private string skinFileName = "~/Themes/zh-cn/Skins/UserRegister.ascx";
        #endregion
        #region 初始化控件
        protected override string GetSkinFileName()
        {
            return skinFileName;
        }
        private string GetTel(string strAreaCode, string strTelNum)
        {
            if (!string.IsNullOrEmpty(strTelNum))
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(strAreaCode);
                stringBuilder.Append("-");
                stringBuilder.Append(strTelNum);
                return stringBuilder.ToString();
            }
            return "";
        }

        private void SendMailToActivation(string strUserName, string strUserKey)
        {
            string webSiteName = ResourceGlobals.Instance.GetString("Site_WebSite");
            string strUrl = string.Format("{0}/UserActivation.aspx?Id={1}&key={2}", webSiteName, strUserKey,
                                          Ue.Framework.Security.DEncryptUtils.MD5EnCode(strUserKey));
            var emailBody = userRegisterConfirmEmailTemplate.Replace("[UserName]", strUserName).Replace("[WebSiteName]", webSiteName).Replace("[UserRegisterConfirmUrl]", strUrl);
            Email.SendMail(strUserName, "帐号注册激活", emailBody);
            MessageWindow.RenderMessage("发送成功,请您在24小时之内前往邮箱激活账号");
        }
        protected override User Enttiy
        {
            get
            {
                if(this.Action=="Delete")
                {
                    return new User(this.EntityKey);
                }
                string strUserName = tbxUserName.GetValue<string>();
                string strPassWord = tbxPassword.GetValue<string>();
                string strPassWordag = tbxPasswordAg.GetValue<string>();
                string strCompany = tbxCompany.GetValue<string>();
                string strComPanyAddress = tbxCompanyAddress.GetValue<string>();
                string strTelAreaCode = tbxTelCode.GetValue<string>();
                string strTelNum = tbxTelphone.GetValue<string>();
                string strMobile = TbxMobile.GetValue<string>();
                if (strPassWord.Equals(strPassWordag))
                {
                    IUserRepository userRepository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
                    if (userRepository.FindBy(strUserName) == null)
                    {
                        User user = new User(strUserName, Ue.Framework.Security.DEncryptUtils.MD5EnCode(strPassWord));
                        user.Profile.Company = strCompany;
                        user.Profile.CompanyAddress = strComPanyAddress;
                        user.Profile.Telephone = GetTel(strTelAreaCode, strTelNum);
                        user.Profile.MobilePhone = strMobile;
                        strUid = user.Key.ToString();
                        SendMailToActivation(user.UserName, user.Key.ToString());
                        return user;
                    }
                    else
                    {
                        MessageWindow.RenderMessage("该用户名已存在");
                    }
                }
                else
                {
                    MessageWindow.RenderMessage("两次输入的密码不同");
                }
                return null;
            }

            set { }

        }
        protected override void InitSkin(Control skin)
        {
            tbxUserName = skin.FindControl<TextBox>("tbxUserName");
            tbxPassword = skin.FindControl<TextBox>("tbxPassword");
            tbxPasswordAg = skin.FindControl<TextBox>("tbxPassWordAg");
            tbxCompany = skin.FindControl<TextBox>("tbxCompany");
            tbxCompanyAddress = skin.FindControl<TextBox>("tbxCompanyAddress");
            tbxTelCode = skin.FindControl<TextBox>("tbxTelCode");
            tbxTelphone = skin.FindControl<TextBox>("tbxTelphone");
            TbxMobile = skin.FindControl<TextBox>("tbxMobile");
        }
        #endregion
    }
}
